package Kruskal;

import java.util.Arrays;

public class KruskalCase {
    private char[] vertex;
    private int[][] matrix;
    private final static int INF = Integer.MAX_VALUE;
    private int edgeNum;

    public static void main(String[] args) {
        char[] vertexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        //克鲁斯卡尔算法的邻接矩阵
        int matrix[][] = {
                /*A*//*B*//*C*//*D*//*E*//*F*//*G*/
                /*A*/ {0, 12, INF, INF, INF, 16, 14},
                /*B*/ {12, 0, 10, INF, INF, 7, INF},
                /*C*/ {INF, 10, 0, 3, 5, 6, INF},
                /*D*/ {INF, INF, 3, 0, 4, INF, INF},
                /*E*/ {INF, INF, 5, 4, 0, 2, 8},
                /*F*/ {16, 7, 6, INF, 2, 0, 9},
                /*G*/ {14, INF, INF, INF, 8, 9, 0}};
        //大家可以在去测试其它的邻接矩阵，结果都可以得到最小生成树.
        KruskalCase kruskalCase = new KruskalCase(vertexs, matrix);
        kruskalCase.print();
        kruskalCase.Kruskal();
    }

    private void print() {
        System.out.println("邻接矩阵\n");
        for (int i = 0; i < vertex.length; i++) {
            for (int j = 0; j < vertex.length; j++) {
                System.out.printf("%12d", matrix[i][j]);
            }
            System.out.println();
        }
    }

    //初始化时，需要传入节点和邻接矩阵数组
    private KruskalCase(char[] vertex, int[][] matrix) {
        //获取到vertex的长度
        int vlen = vertex.length;

        //初始化，复制拷贝的方式
        this.vertex = new char[vlen];
        for (int i = 0; i < vertex.length; i++) {
            this.vertex[i] = vertex[i];
        }

        //初始化边,其实就是初始化matrix
        this.matrix = new int[vlen][vlen];
        for (int i = 0; i < vlen; i++) {
            for (int j = 0; j < vlen; j++) {
                this.matrix[i][j] = matrix[i][j];
            }
        }
        //统计边
        for (int i = 0; i < vlen; i++) {
            for (int j = i + 1; j < vlen; j++) {
                if (this.matrix[i][j] != INF) {
                    edgeNum++;
                }
            }
        }
    }

    public void Kruskal() {
        int index = 0;//表示最后结果数组的索引
        int[] ends = new int[edgeNum];//记录最小生成树中每个顶点的终点，用于判断回路
        EData[] rets = new EData[edgeNum];


        //获取图中所有边的集合
        EData[] edges = getEdge();
        //输出一波
        System.out.println("排序前，共" + edges.length + "\n图的边的集合" + Arrays.toString(edges));

        //了解了边的情况
        //需要对边进行排序
        sortEdge(edges);
        //输出一波
        System.out.println("排序后，共" + edges.length + "\n图的边的集合" + Arrays.toString(edges));
        //知道了边的情况，那么我们就要开始办正事了。
        //开始遍历边
        for (int i = 0; i < edgeNum; i++) {
            //遍历到的边的起点，获取到他的坐标
            int p1 = getPosition(edges[i].start);
            //遍历到的终点，获取坐标
            int p2 = getPosition(edges[i].end);
            //获取到该节点的终点
            int m = getEnd(ends, p1);
            int n = getEnd(ends, p2);
            if (m != n){
                //终点不一致，则加入最小生成树中，表面这两个点在最小生成树中为连接关系
                ends[m]=n;
                //将该边加入到最小生成树中
                rets[index++]=edges[i];
            }
        }

    }

    private int getEnd(int[] ends, int i) {
        while (ends[i] != 0) {
            i = ends[i];
        }
        return i;
    }

    private int getPosition(char ch) {
        for (int i = 0; i < vertex.length; i++) {
            if (vertex[i] == ch) {//找到
                return i;
            }
        }
        //找不到,返回-1
        return -1;
    }

    //对边进行排序,此处采用冒泡排序
    private void sortEdge(EData[] edges) {
        for (int i = 0; i < edges.length - 1; i++) {
            for (int j = 0; j < edges.length - i - 1; j++) {
                if (edges[j].weight > edges[j + 1].weight) {
                    //前面的比后面的大。往后换
                    EData temp = edges[j];
                    edges[j] = edges[j + 1];
                    edges[j + 1] = temp;
                }
            }
        }
    }

    //获取图中所有边的集合
    private EData[] getEdge() {
        int index = 0;
        EData[] edges = new EData[edgeNum];
        for (int i = 0; i < vertex.length; i++) {
            for (int j = i + 1; j < vertex.length; j++) {
                if (matrix[i][j] != INF) {
                    edges[index++] = new EData(vertex[i], vertex[j], matrix[i][j]);
                }
            }
        }
        return edges;
    }
}

class EData {
    char start;
    char end;
    int weight;

    public EData(char start, char end, int weight) {
        this.start = start;
        this.end = end;
        this.weight = weight;
    }

    //重写toString方法

    @Override
    public String toString() {
        return "EData [<" + start + ", " + end + ">= " + weight + "]";
    }
}
